package com.example.lenovo.zyfypt_221yln.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import com.example.lenovo.zyfypt_221yln.R;
import com.example.lenovo.zyfypt_221yln.bean.LoginBean;
import com.example.lenovo.zyfypt_221yln.bean.SessionID;
import com.example.lenovo.zyfypt_221yln.iface.LoginListener;
import com.example.lenovo.zyfypt_221yln.model.LoginModel;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
    private Button btn,btn1;
    private EditText idET,pwdEt;
    private String username,password;
    private Switch sw;
    SharedPreferences sp;

    private LoginListener loginListener = new LoginListener() {
        @Override
        public void onResponse(LoginBean loginBean) {
            System.out.println("result:"+loginBean.getId());
            SessionID.SessionID = loginBean.getSessionid();
            SessionID.id = loginBean.getId();
            SessionID.realname = loginBean.getRealname();
            SessionID.rolename = loginBean.getRolename();
            SessionID.username = loginBean.getUsername();
            SessionID.sex = loginBean.getSex();
            if(loginBean.getId()!=null) {
                Intent intent1 = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent1);
            }
        }

        @Override
        public void onFail(String msg) {
            System.out.println("fail");
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Toast.makeText(this, "登录", Toast.LENGTH_SHORT).show();
        init();
    }

    private void init() {
        btn=(Button)findViewById(R.id.button);
        btn1=(Button)findViewById(R.id.button2);
        idET = (EditText)findViewById(R.id.editText);
        pwdEt = (EditText)findViewById(R.id.editText2);


        btn.setOnClickListener(this);
        btn1.setOnClickListener(this);

        sw = (Switch) findViewById(R.id.switch1);
        sw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(sw.isChecked()){
                    saveSP();
                } else {
                    idET.setText("");
                    pwdEt.setText("");
                    saveSP();
                }
            }
        });

        sp = getSharedPreferences("login",MODE_PRIVATE);
        readSP();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                System.out.println("点击登录按钮！");
                LoginModel loginModel = new LoginModel();
                loginModel.getResult(idET.getText().toString(),
                        pwdEt.getText().toString(),
                        loginListener);
                break;
            case R.id.button2:
                System.out.println("点击注册按钮！");
                Intent intent = new Intent();
                intent.setClass(this, RegisterActivity.class);
                startActivity(intent);
                break;

        }
    }

    private void readSP() {
        String name = sp.getString("username",null);
        String pass = sp.getString("password",null);
        Boolean b = sp.getBoolean("remember",false);
        if(b){
            idET.setText(name);
            pwdEt.setText(pass);
            sw.setChecked(b);
        }
    }

    private void saveSP() {
        SharedPreferences.Editor editor = sp.edit();
        username = idET.getText().toString();
        password = pwdEt.getText().toString();
        editor.putString("username",username);
        editor.putString("password",password);
        editor.putBoolean("rememeber",sw.isChecked());
        editor.commit();
    }
}
